home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / gdb35src.zoo / dist-gdb / core.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-05  |  14.1 KB  |  564 lines

  1. /* Work with core dump and executable files, for GDB.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "frame.h"  /* required by inferior.h */
  24. #include "inferior.h"
  25.  
  26. #ifdef USG
  27. #include <sys/types.h>
  28. #include <fcntl.h>
  29. #endif
  30.  
  31. #ifdef COFF_ENCAPSULATE
  32. #include "a.out.encap.h"
  33. #else
  34. #include <a.out.h>
  35. #endif
  36. #ifndef N_MAGIC
  37. #ifdef COFF_FORMAT
  38. #define N_MAGIC(exec) ((exec).magic)
  39. #else
  40. #define N_MAGIC(exec) ((exec).a_magic)
  41. #endif
  42. #endif
  43. #include <signal.h>
  44. #include <sys/param.h>
  45. #include <sys/dir.h>
  46. #include <sys/file.h>
  47. #include <sys/stat.h>
  48.  
  49. #ifndef atarist
  50. #ifdef UMAX_CORE
  51. #include <sys/ptrace.h>
  52. #else
  53. #include <sys/user.h>
  54. #endif
  55. #endif
  56.  
  57. #ifndef N_TXTADDR
  58. #define N_TXTADDR(hdr) 0
  59. #endif /* no N_TXTADDR */
  60.  
  61. #ifndef N_DATADDR
  62. #define N_DATADDR(hdr) hdr.a_text
  63. #endif /* no N_DATADDR */
  64.  
  65. #ifndef COFF_FORMAT
  66. #ifndef AOUTHDR
  67. #define AOUTHDR        struct exec
  68. #endif
  69. #endif
  70.  
  71. extern char *sys_siglist[];
  72.  
  73. extern core_file_command (), exec_file_command ();
  74.  
  75. /* Hook for `exec_file_command' command to call.  */
  76.  
  77. void (*exec_file_display_hook) ();
  78.    
  79. /* File names of core file and executable file.  */
  80.  
  81. char *corefile;
  82. char *execfile;
  83.  
  84. /* Descriptors on which core file and executable file are open.
  85.    Note that the execchan is closed when an inferior is created
  86.    and reopened if the inferior dies or is killed.  */
  87.  
  88. int corechan;
  89. int execchan;
  90.  
  91. /* Last modification time of executable file.
  92.    Also used in source.c to compare against mtime of a source file.  */
  93.  
  94. int exec_mtime;
  95.  
  96. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  97.  
  98. CORE_ADDR data_start;
  99. CORE_ADDR data_end;
  100. CORE_ADDR stack_start;
  101. CORE_ADDR stack_end;
  102.  
  103. #if defined (REG_STACK_SEGMENT)
  104. /* Start and end of the register stack segment.  */
  105. CORE_ADDR reg_stack_start;
  106. CORE_ADDR reg_stack_end;
  107. #endif /* REG_STACK_SEGMENT */
  108.  
  109. /* Virtual addresses of bounds of two areas of memory in the exec file.
  110.    Note that the data area in the exec file is used only when there is no core file.  */
  111.  
  112. CORE_ADDR text_start;
  113. CORE_ADDR text_end;
  114.  
  115. CORE_ADDR exec_data_start;
  116. CORE_ADDR exec_data_end;
  117.  
  118. /* Offset within executable file of start of text area data.  */
  119.  
  120. int text_offset;
  121.  
  122. /* Offset within executable file of start of data area data.  */
  123.  
  124. int exec_data_offset;
  125.  
  126. /* Offset within core file of start of data area data.  */
  127.  
  128. int data_offset;
  129.  
  130. /* Offset within core file of start of stack area data.  */
  131.  
  132. int stack_offset;
  133.   
  134. #ifdef COFF_FORMAT
  135. /* various coff data structures */
  136.  
  137. FILHDR file_hdr;
  138. SCNHDR text_hdr;
  139. SCNHDR data_hdr;
  140.  
  141. #endif /* not COFF_FORMAT */
  142.  
  143. /* a.out header saved in core file.  */
  144.   
  145. AOUTHDR core_aouthdr;
  146.  
  147. /* a.out header of exec file.  */
  148.  
  149. AOUTHDR exec_aouthdr;
  150.  
  151. void validate_files ();
  152. unsigned int register_addr ();
  153.  
  154. /* Call this to specify the hook for exec_file_command to call back.
  155.    This is called from the x-window display code.  */
  156.  
  157. void
  158. specify_exec_file_hook (hook)
  159.      void (*hook) ();
  160. {
  161.   exec_file_display_hook = hook;
  162. }
  163.  
  164. /* The exec file must be closed before running an inferior.
  165.    If it is needed again after the inferior dies, it must
  166.    be reopened.  */
  167.  
  168. void
  169. close_exec_file ()
  170. {
  171.   if (execchan >= 0)
  172.     close (execchan);
  173.   execchan = -1;
  174. }
  175.  
  176. void
  177. reopen_exec_file ()
  178. {
  179.   if (execchan < 0 && execfile != 0)
  180.     {
  181.       char *filename = concat (execfile, "", "");
  182.       exec_file_command (filename, 0);
  183.       free (filename);
  184.     }
  185. }
  186.  
  187. /* If we have both a core file and an exec file,
  188.    print a warning if they don't go together.
  189.    This should really check that the core file came
  190.    from that exec file, but I don't know how to do it.  */
  191.  
  192. void
  193. validate_files ()
  194. {
  195.   if (execfile != 0 && corefile != 0)
  196.     {
  197.       struct stat st_core;
  198.  
  199.       if (fstat (corechan, &st_core) < 0)
  200.     /* It might be a good idea to print an error message.
  201.        On the other hand, if the user tries to *do* anything with
  202.        the core file, (s)he'll find out soon enough.  */
  203.     return;
  204.  
  205.       if (N_MAGIC (core_aouthdr) != 0
  206.       && bcmp (&core_aouthdr, &exec_aouthdr, sizeof core_aouthdr))
  207.     printf ("Warning: core file does not match specified executable file.\n");
  208.       else if (exec_mtime > st_core.st_mtime)
  209.     printf ("Warning: exec file is newer than core file.\n");
  210.     }
  211. }
  212.  
  213. /* Return the name of the executable file as a string.
  214.    ERR nonzero means get error if there is none specified;
  215.    otherwise return 0 in that case.  */
  216.  
  217. char *
  218. get_exec_file (err)
  219.      int err;
  220. {
  221.   if (err && execfile == 0)
  222.     error ("No executable file specified.\n\
  223. Use the \"exec-file\" and \"symbol-file\" commands.");
  224.   return execfile;
  225. }
  226.  
  227. int
  228. have_core_file_p ()
  229. {
  230.   return corefile != 0;
  231. }
  232.  
  233. static void
  234. files_info ()
  235. {
  236.   char *symfile;
  237.   extern char *get_sym_file ();
  238.  
  239.   if (execfile)
  240.     printf ("Executable file \"%s\".\n", execfile);
  241.   else
  242.     printf ("No executable file\n");
  243.   if (corefile == 0)
  244.     printf ("No core dump file\n");
  245.   else
  246.     printf ("Core dump file \"%s\".\n", corefile);
  247.  
  248.   if (have_inferior_p ())
  249.     printf ("Using the running image of the program, rather than these files.\n");
  250.  
  251.   symfile = get_sym_file ();
  252.   if (symfile != 0)
  253.     printf ("Symbols from \"%s\".\n", symfile);
  254.  
  255. #ifdef FILES_INFO_HOOK
  256.   if (FILES_INFO_HOOK ())
  257.     return;
  258. #endif
  259.  
  260.   if (! have_inferior_p ())
  261.     {
  262.       if (execfile)
  263.     {
  264.       printf ("Text segment in executable from 0x%x to 0x%x.\n",
  265.           text_start, text_end);
  266.       printf ("Data segment in executable from 0x%x to 0x%x.\n",
  267.           exec_data_start, exec_data_end);
  268.       if (corefile)
  269.         printf ("(But since we have a core file, we're using...)\n");
  270.     }
  271.       if (corefile)
  272.     {
  273.       printf ("Data segment in core file from 0x%x to 0x%x.\n",
  274.           data_start, data_end);
  275.       printf ("Stack segment in core file from 0x%x to 0x%x.\n",
  276.           stack_start, stack_end);
  277.     }
  278.     }
  279. }
  280.  
  281. /* Read "memory data" from core file and/or executable file.
  282.    Returns zero if successful, 1 if xfer_core_file failed, errno value if
  283.    ptrace failed. */
  284.  
  285. int
  286. read_memory (memaddr, myaddr, len)
  287.      CORE_ADDR memaddr;
  288.      char *myaddr;
  289.      int len;
  290. {
  291.   if (len == 0)
  292.     return 0;
  293.  
  294.   if (have_inferior_p ())
  295.     {
  296.       if (remote_debugging)
  297.     return remote_read_inferior_memory (memaddr, myaddr, len);
  298.       else
  299.     return read_inferior_memory (memaddr, myaddr, len);
  300.     }
  301.   else
  302.       return xfer_core_file (memaddr, myaddr, len);
  303. }
  304.  
  305. /* Write LEN bytes of data starting at address MYADDR
  306.    into debugged program memory at address MEMADDR.
  307.    Returns zero if successful, or an errno value if ptrace failed.  */
  308.  
  309. int
  310. write_memory (memaddr, myaddr, len)
  311.      CORE_ADDR memaddr;
  312.      char *myaddr;
  313.      int len;
  314. {
  315.   if (have_inferior_p ())
  316.     {
  317.       if (remote_debugging)
  318.     return remote_write_inferior_memory (memaddr, myaddr, len);
  319.       else
  320.     return write_inferior_memory (memaddr, myaddr, len);
  321.     }
  322.   else
  323.     error ("Can write memory only when program being debugged is running.");
  324. }
  325.  
  326. #ifndef XFER_CORE_FILE
  327. /* Read from the program's memory (except for inferior processes).
  328.    This function is misnamed, since it only reads, never writes; and
  329.    since it will use the core file and/or executable file as necessary.
  330.  
  331.    It should be extended to write as well as read, FIXME, for patching files.
  332.  
  333.    Return 0 if address could be read, 1 if not. */
  334.  
  335. int
  336. xfer_core_file (memaddr, myaddr, len)
  337.      CORE_ADDR memaddr;
  338.      char *myaddr;
  339.      int len;
  340. {
  341.   register int i;
  342.   register int val;
  343.   int xferchan;
  344.   char **xferfile;
  345.   int fileptr;
  346.   int returnval = 0;
  347.  
  348.   while (len > 0)
  349.     {
  350.       xferfile = 0;
  351.       xferchan = 0;
  352.  
  353.       /* Determine which file the next bunch of addresses reside in,
  354.      and where in the file.  Set the file's read/write pointer
  355.      to point at the proper place for the desired address
  356.      and set xferfile and xferchan for the correct file.
  357.  
  358.      If desired address is nonexistent, leave them zero.
  359.  
  360.      i is set to the number of bytes that can be handled
  361.      along with the next address.
  362.  
  363.      We put the most likely tests first for efficiency.  */
  364.  
  365.       /* Note that if there is no core file
  366.      data_start and data_end are equal.  */
  367.       if (memaddr >= data_start && memaddr < data_end)
  368.     {
  369.       i = min (len, data_end - memaddr);
  370.       fileptr = memaddr - data_start + data_offset;
  371.       xferfile = &corefile;
  372.       xferchan = corechan;
  373.     }
  374.       /* Note that if there is no core file
  375.      stack_start and stack_end are equal.  */
  376.       else if (memaddr >= stack_start && memaddr < stack_end)
  377.     {
  378.       i = min (len, stack_end - memaddr);
  379.       fileptr = memaddr - stack_start + stack_offset;
  380.       xferfile = &corefile;
  381.       xferchan = corechan;
  382.     }
  383. #ifdef REG_STACK_SEGMENT
  384.       /* Pyramids have an extra segment in the virtual address space
  385.          for the (control) stack of register-window frames */
  386.       else if (memaddr >= reg_stack_start && memaddr < reg_stack_end)
  387.     {
  388.       i = min (len, reg_stack_end - memaddr);
  389.       fileptr = memaddr - reg_stack_start + reg_stack_offset;
  390.       xferfile = &corefile;
  391.       xferchan = corechan;
  392.     }
  393. #endif /* REG_STACK_SEGMENT */
  394.  
  395.       else if (corechan < 0
  396.            && memaddr >= exec_data_start && memaddr < exec_data_end)
  397.     {
  398.       i = min (len, exec_data_end - memaddr);
  399.       fileptr = memaddr - exec_data_start + exec_data_offset;
  400.       xferfile = &execfile;
  401.       xferchan = execchan;
  402.     }
  403.       else if (memaddr >= text_start && memaddr < text_end)
  404.     {
  405.       i = min (len, text_end - memaddr);
  406.       fileptr = memaddr - text_start + text_offset;
  407.       xferfile = &execfile;
  408.       xferchan = execchan;
  409.     }
  410.       else if (memaddr < text_start)
  411.     {
  412.       i = min (len, text_start - memaddr);
  413.     }
  414.       else if (memaddr >= text_end
  415.            && memaddr < (corechan >= 0? data_start : exec_data_start))
  416.     {
  417.       i = min (len, data_start - memaddr);
  418.     }
  419.       else if (corechan >= 0
  420.            && memaddr >= data_end && memaddr < stack_start)
  421.     {
  422.       i = min (len, stack_start - memaddr);
  423.     }
  424.       else if (corechan < 0 && memaddr >= exec_data_end)
  425.     {
  426.       /* Since there is nothing at higher addresses than data
  427.          (without a core file or an inferior, there is no
  428.          stack, set i to do the rest of the operation now.  */
  429.       i = len;
  430.     }
  431. #ifdef REG_STACK_SEGMENT
  432.       else if (memaddr >= reg_stack_end && reg_stack_end != 0)
  433.     {
  434.       i = min (len, reg_stack_start - memaddr);
  435.     }
  436.       else if (memaddr >= stack_end && memaddr < reg_stack_start)
  437. #else /* no REG_STACK_SEGMENT.  */
  438.       else if (memaddr >= stack_end && stack_end != 0)
  439. #endif /* no REG_STACK_SEGMENT.  */
  440.     {
  441.       /* Since there is nothing at higher addresses than
  442.          the stack, set i to do the rest of the operation now.  */
  443.       i = len;
  444.     }
  445.       else
  446.     {
  447.       /* Address did not classify into one of the known ranges.
  448.          This shouldn't happen; we catch the endpoints.  */
  449.       fatal ("Internal: Bad case logic in xfer_core_file.");
  450.     }
  451.  
  452.       /* Now we know which file to use.
  453.      Set up its pointer and transfer the data.  */
  454.       if (xferfile)
  455.     {
  456.       if (*xferfile == 0)
  457.         if (xferfile == &execfile)
  458.           error ("No program file to examine.");
  459.         else
  460.           error ("No core dump file or running program to examine.");
  461.       val = lseek (xferchan, fileptr, 0);
  462.       if (val < 0)
  463.         perror_with_name (*xferfile);
  464.       val = myread (xferchan, myaddr, i);
  465.       if (val < 0)
  466.         perror_with_name (*xferfile);
  467.     }
  468.       /* If this address is for nonexistent memory,
  469.      read zeros if reading, or do nothing if writing.
  470.      Actually, we never right.  */
  471.       else
  472.     {
  473.       bzero (myaddr, i);
  474.       returnval = 1;
  475.     }
  476.  
  477.       memaddr += i;
  478.       myaddr += i;
  479.       len -= i;
  480.     }
  481.   return returnval;
  482. }
  483. #endif /* XFER_CORE_FILE */
  484.  
  485. /* My replacement for the read system call.
  486.    Used like `read' but keeps going if `read' returns too soon.  */
  487.  
  488. int
  489. myread (desc, addr, len)
  490.      int desc;
  491.      char *addr;
  492.      int len;
  493. {
  494.   register int val;
  495.   int orglen = len;
  496.  
  497.   while (len > 0)
  498.     {
  499.       val = read (desc, addr, len);
  500.       if (val < 0)
  501.     return val;
  502.       if (val == 0)
  503.     return orglen - len;
  504.       len -= val;
  505.       addr += val;
  506.     }
  507.   return orglen;
  508. }
  509.  
  510. #ifdef REGISTER_U_ADDR
  511.  
  512. /* Return the address in the core dump or inferior of register REGNO.
  513.    BLOCKEND is the address of the end of the user structure.  */
  514.  
  515. unsigned int
  516. register_addr (regno, blockend)
  517.      int regno;
  518.      int blockend;
  519. {
  520.   int addr;
  521.  
  522.   if (regno < 0 || regno >= NUM_REGS)
  523.     error ("Invalid register number %d.", regno);
  524.  
  525.   REGISTER_U_ADDR (addr, blockend, regno);
  526.  
  527.   return addr;
  528. }
  529.  
  530. #endif /* REGISTER_U_ADDR */
  531.  
  532. void
  533. _initialize_core()
  534. {
  535.   corechan = -1;
  536.   execchan = -1;
  537.   corefile = 0;
  538.   execfile = 0;
  539.   exec_file_display_hook = 0;
  540.  
  541.   text_start = 0;
  542.   text_end = 0;
  543.   data_start = 0;
  544.   data_end = 0;
  545.   exec_data_start = 0;
  546.   exec_data_end = 0;
  547.   stack_start = STACK_END_ADDR;
  548.   stack_end = STACK_END_ADDR;
  549.  
  550. #ifndef atarist
  551.   add_com ("core-file", class_files, core_file_command,
  552.        "Use FILE as core dump for examining memory and registers.\n\
  553. No arg means have no core file.");
  554. #endif
  555.  
  556.   add_com ("exec-file", class_files, exec_file_command,
  557.        "Use FILE as program for getting contents of pure memory.\n\
  558. If FILE cannot be found as specified, your execution directory path\n\
  559. is searched for a command of that name.\n\
  560. No arg means have no executable file.");
  561.   add_info ("files", files_info, "Names of files being debugged.");
  562. }
  563.  
  564.